package pxene.phantomJs;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

import pxene.phantomJs.util.DriverUtils;
import pxene.phantomJs.util.SingleDriver;

public class PhantomJs4TouTiao {
	private static final String userAgent = "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1";
	static {
		System.setProperty("phantomjs.binary.path",
				"C:/Users/chenjinghui/Desktop/phantomjs-2.1.1-windows/bin/phantomjs.exe");
		System.setProperty("phantomjs.page.settings.userAgent", userAgent);
	}
	
	public static void main(String[] args) throws Exception {
//		SingleDriver selenium = new SingleDriver();
//		WebDriver driver = SingleDriver.getInstance();
		
		DesiredCapabilities dCaps = DesiredCapabilities.phantomjs();
		dCaps.setJavascriptEnabled(true);
		dCaps.setCapability("takesScreenshot", false);
		
//		DesiredCapabilities dCaps = new DesiredCapabilities();
//		dCaps.setJavascriptEnabled(true);
		
//		DesiredCapabilities caps = new DesiredCapabilities();
//		caps.setJavascriptEnabled(true);                
//		caps.setCapability("takesScreenshot", true);  
//		caps.setCapability(
//		                        PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
//		                        "C:\\Users\\chenjinghui\\Desktop\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe"
//		                    );
//		PhantomJSDriver driver = new  PhantomJSDriver(caps);
		
		PhantomJSDriver driver = new PhantomJSDriver(dCaps);
		
//		driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
		
		driver.get("http://www.toutiao.com/");

		// 控制滚动条一直向下翻页
//		JavascriptExecutor js = (JavascriptExecutor) driver;
		int count=0;
		while (true) {
			int size0 = driver.getPageSource().length();
			//模拟人的行为，滑动两下
//			js.executeScript("var q=document.documentElement.scrollTop=1500");
			
			driver.executePhantomJS("var q=document.documentElement.scrollTop=1500");
			
			Thread.sleep(3000);
			
			driver.executePhantomJS("var q=document.documentElement.scrollTop=1000000");
			
			Thread.sleep(4000);
			
//			js.executeScript("var q=document.documentElement.scrollTop=100000");
			int size2 = driver.getPageSource().length();
			
			if (size2 == size0) {
				System.out.println("异步加载结束。。");
				break;
			}
			if(count++>5){
				break;
			}
			List<WebElement> boxes = driver.findElements(By.xpath("//div[@class='item-inner y-box']"));
			for (WebElement box : boxes) {
				
				String title =DriverUtils.getElemText(box, By.xpath(".//a[@class='link title']"));
				String href =DriverUtils.getElemAttr(box, By.xpath(".//a[@class='link title']"),"href");
				String tag = DriverUtils.getElemText(box, By.xpath(".//a[@ga_event='article_tag_click']"));
				String comNum =DriverUtils.getElemText(box, By.xpath(".//a[@class='lbtn comment']"));
				String source =DriverUtils.getElemText(box, By.xpath(".//a[@class='lbtn source']"));
				
				System.out.println("title::::"+title);
				System.out.println("href::::"+href);
				System.out.println("tag::::"+tag);
				System.out.println("comNum::::"+comNum);
				System.out.println("source::::"+source);
			}
			
			System.out.println(count);
		}
		
		
		
		System.out.println(".........over");
		driver.quit();
	}
	
	
	
}
